﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Items_AddEditItems : System.Web.UI.Page
{
    CommonMethods _cMethods = new CommonMethods();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCategory();
            BindSubCategory();
            BindManuacturer();
            populateForm();
        }
    }
    private void BindCategory()
    {
        int companyId = 1;
        DDL_Category.DataSource = _cMethods.getCategory(companyId);
        DDL_Category.DataBind();
    }
    private void BindSubCategory()
    {
        if (DDL_Category.Items.Count > 0)
        {
            DDL_SubCategory.DataSource = _cMethods.getSubCategory(Convert.ToInt32(DDL_Category.SelectedValue));
            DDL_SubCategory.DataBind();
        }
    }
    private void BindManuacturer()
    {
        DDL_Manufacturer.DataSource = _cMethods.getManufacturer();
        DDL_Manufacturer.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Item item = new Item();
        using (POSEntities db = new POSEntities())
        {
            if (btnSave.Text == "Save")
            {
                item = new Item();
            }
            else if (btnSave.Text == "Update")
            {
                int id = Convert.ToInt32(btnSave.CommandArgument);
                item = (from i in db.Items where i.ItemId == id select i).FirstOrDefault();
            }
            item.CategoryId = Convert.ToInt32(DDL_Category.SelectedValue);
            item.SubCategoryId = Convert.ToInt32(DDL_SubCategory.SelectedValue);
            item.ManufacturerId = Convert.ToInt32(DDL_Manufacturer.SelectedValue);
            item.CompanyId = 1;
            item.BranchId = 1;
            item.ItemCode = txtItemCode.Text;
            item.ItemName = txtItemName.Text;
            item.Description = txtDescription.Text;
            item.BarCode = txtBarCode.Text;
            item.SalePrice = Convert.ToDecimal(txtSalePrice.Text);
            item.Discount = Convert.ToInt32(txtDiscount.Text);

            if (btnSave.Text == "Save")
            {
                item.EntryBy = 1;
                item.EntryDate = DateTime.Now;
                db.Items.Add(item);
            }
            else if (btnSave.Text == "Update")
            {
                item.ModifiedBy = 1;
                item.ModifiedDate = DateTime.Now;
            }
            db.SaveChanges();
            clearForm();
            if (Request.QueryString["Id"]!="" && Request.QueryString["Id"]!=null)
            {
                populateForm();
            }
        }
    }

    protected void DDL_Category_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubCategory();
    }

    private void clearForm()
    {
        txtBarCode.Text = "";
        txtDescription.Text = "";
        txtDiscount.Text = "";
        txtItemCode.Text = "";
        txtItemName.Text = "";
        txtSalePrice.Text = "";
       
    }

    private void populateForm()
    {
        if (Request.QueryString["Id"]!="" && Request.QueryString["Id"]!=null)
        {
            using (POSEntities db = new POSEntities())
            {
                int id = Convert.ToInt32(Request.QueryString["Id"]);
                Item item = (from i in db.Items where i.ItemId == id select i).FirstOrDefault();
                DDL_Category.SelectedValue = item.CategoryId.ToString();
                BindSubCategory();
                DDL_SubCategory.SelectedValue = item.SubCategoryId.ToString();
                DDL_Manufacturer.SelectedValue = item.ManufacturerId.ToString();
                txtBarCode.Text = item.BarCode;
                txtDescription.Text = item.Description;
                txtDiscount.Text = item.Discount.ToString();
                txtItemCode.Text = item.ItemCode;
                txtItemName.Text = item.ItemName;
                txtSalePrice.Text = item.SalePrice.ToString();
                btnSave.Text = "Update";
                btnSave.CommandArgument = item.ItemId.ToString();
            }
        }
    }
}